//
//  CYWorksMyArcitleViewController.m
//  cyydt
//
//  Created by chu on 2019/1/21.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYWorksMyArcitleViewController.h"

#import "RichTextEditViewController.h"
#import "CYWorksMyArcitle_AllViewController.h"
#import "CYWorksMyArcitle_ToBeViewController.h"
#import "CYWorksMyArcitle_NotPassViewController.h"
#import "CYWorksMyArcitle_HavePassViewController.h"
#import "CYWorksMyArcitle_CaoGaoViewController.h"


@interface CYWorksMyArcitleViewController ()<SGTopScrollMenuDelegate, UIScrollViewDelegate>

@property (nonatomic, strong) SGTopScrollMenu *topMenu;

@property (nonatomic, strong) UIScrollView *mainScrollView;

@end

@implementation CYWorksMyArcitleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的文章";
    // 创建底部滚动视图
    self.mainScrollView = [[UIScrollView alloc] init];
    _mainScrollView.frame = CGRectMake(0, 44, kWindowW, kWindowH - Height_NavBar - 44);
    _mainScrollView.contentSize = CGSizeMake(self.view.frame.size.width * 5, 0);
    _mainScrollView.backgroundColor = [UIColor clearColor];
    // 开启分页
    _mainScrollView.pagingEnabled = YES;
    // 没有弹簧效果
    _mainScrollView.bounces = NO;
    // 隐藏水平滚动条
    _mainScrollView.showsHorizontalScrollIndicator = NO;
    // 设置代理
    _mainScrollView.delegate = self;
    [self.view addSubview:_mainScrollView];
    
    [self setChildViewControllers];
    
    [self.view addSubview:self.topMenu];
    
    [self rightBarItemWithTitle:@"发布"];
}

- (void)RighttouchEvent{
    RichTextEditViewController *video = [[RichTextEditViewController alloc] init];
    [[AppDelegate sharedAppDelegate] pushViewController:video];
}

// 显示控制器的view
- (void)showVc:(NSInteger)index {
    
    CGFloat offsetX = index * self.view.frame.size.width;
    
    UIViewController *vc = self.childViewControllers[index];
    // 1 计算滚动的位置
    self.mainScrollView.contentOffset = CGPointMake(offsetX, 0);
    
    // 判断控制器的view有没有加载过,如果已经加载过,就不需要加载
    if (vc.isViewLoaded) return;
    
    [self.mainScrollView addSubview:vc.view];
    vc.view.frame = CGRectMake(offsetX, 0, self.view.frame.size.width, self.mainScrollView.frame.size.height);
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    // 计算滚动到哪一页
    NSInteger index = scrollView.contentOffset.x / scrollView.bounds.size.width;
    // 1.添加子控制器view
    [self showVc:index];
    [self.topMenu selectLabel:self.topMenu.allTitleLabel[index]];
}

#pragma mark - SGTopScrollMenuDelegate
- (void)SGTopScrollMenu:(SGTopScrollMenu *)topScrollMenu didSelectTitleAtIndex:(NSInteger)index{
    [self showVc:index];
    
}

- (void)setChildViewControllers{
    CYWorksMyArcitle_AllViewController *commend = [[CYWorksMyArcitle_AllViewController alloc] init];
    commend.view.frame = CGRectMake(0, 0, kWindowW, self.mainScrollView.frame.size.height);
    [self.mainScrollView addSubview:commend.view];
    [self addChildViewController:commend];
    
    CYWorksMyArcitle_CaoGaoViewController *info = [[CYWorksMyArcitle_CaoGaoViewController alloc] init];
    [self addChildViewController:info];
    
    CYWorksMyArcitle_ToBeViewController *video = [[CYWorksMyArcitle_ToBeViewController alloc] init];
    [self addChildViewController:video];
    
    CYWorksMyArcitle_HavePassViewController *projevt = [[CYWorksMyArcitle_HavePassViewController alloc] init];
    [self addChildViewController:projevt];
    
    CYWorksMyArcitle_NotPassViewController *notpass = [[CYWorksMyArcitle_NotPassViewController alloc] init];
    [self addChildViewController:notpass];
}

- (SGTopScrollMenu *)topMenu{
    if (!_topMenu) {
        _topMenu = [SGTopScrollMenu topScrollMenuWithFrame:CGRectMake(0, 0, kWindowW , 44) NormalColor:RGBOF(0x999999) SelColor:RGBOF(0xF43636)];
        _topMenu.topScrollMenuDelegate = self;
        _topMenu.titlesArr = @[@"全部", @"草稿", @"待审核", @"通过", @"未通过"];
    }
    return _topMenu;
}

@end
